package com.cloudbroker.openapi.mobile.controller;

import static com.cloudbroker.bcs.platform.dav.bizapi.constants.WechatUrl.GET_ACCESS_TOKEN_BY_CODE;
import static com.cloudbroker.bcs.platform.dav.bizapi.constants.WechatUrl.GET_USERINFO_BY_ACCESS_TOKEN;
import static com.cloudbroker.bcs.platform.dav.common.util.RequestUtil.getSingleParameterMapForBody;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.cloudbroker.bcs.platform.dav.bizapi.service.business.wechat.vo.WechatCodeTenant;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.wechat.result.UserInfoByCode;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.wechat.vo.PayForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.wechat.vo.WechatCode;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.response.TongLianPayBackVo;
import com.cloudbroker.bcs.platform.dav.bizapi.t2service.business.FansT2BizService;
import com.cloudbroker.bcs.platform.dav.bizapi.t2service.business.WechatT2BizService;
import com.cloudbroker.bcs.platform.dav.common.ext.annotation.SaveLog;
import com.cloudbroker.openapi.mobile.util.HttpUtil;

/**
 * Created by wangxu13798 on 2016/3/9.
 */
@Controller
public class WeixinController {

    @Resource
    private WechatT2BizService wechatT2;
    @Resource
    private FansT2BizService fansT2;

    /**
     * 通过code获取access_token
     * */
    @ResponseBody
    @RequestMapping("/get_access_token_by_code")
    @SaveLog(remark = "通过code获取access_token")
    public Map<String,Object> getAccessTokenByCode(HttpServletRequest request , String jsonpcallback){
        Map<String,Object> param = getSingleParameterMapForBody(request);
        param.put("appid", "wxaedbfa0dd03a2b26");
        param.put("secret", "c410103b31f67fb55b859a14a52ffb13");
        param.put("grant_type", "authorization_code");
        String str = HttpUtil.doGet(GET_ACCESS_TOKEN_BY_CODE, (Map<String, Object>) param);
        return VOUtil.setJsonpResult(JSONObject.parseObject(str), jsonpcallback);
    }

    /**
     * 使用access_token获取用户信息
     * */
    @ResponseBody
    @RequestMapping("/get_userinfo_by_access_token")
    @SaveLog(remark = "使用access_token获取用户信息")
    public Map<String,Object> getUserinfoByAccessToken(HttpServletRequest request , String jsonpcallback){
        String str = HttpUtil.doGet(GET_USERINFO_BY_ACCESS_TOKEN, getSingleParameterMapForBody(request));
        return VOUtil.setJsonpSuccessResult(JSONObject.parseObject(str) , jsonpcallback);
    }

    /**
     * 使用code获取用户信息
     * */
    @ResponseBody
    @RequestMapping("/func_get_userinfo_by_code")
    @SaveLog(remark = "使用code获取用户信息")
    public UserInfoByCode getUserinfoByCode(HttpServletRequest request , String jsonpcallback){
        WechatCode form = getSingleParameterMapForBody(request , WechatCode.class);
        return VOUtil.setJsonpResult(wechatT2.getUserinfoByCode(form), jsonpcallback);
    }

    /**
     * 使用code和租户信息获取用户信息
     * */
    @ResponseBody
    @RequestMapping("/func_get_userinfo_by_code_tenant")
    @SaveLog(remark = "使用code和租户信息获取用户信息")
    public UserInfoByCode getUserinfoByCodeTenant(HttpServletRequest request , String jsonpcallback){
        WechatCodeTenant form = getSingleParameterMapForBody(request , WechatCodeTenant.class);
        return VOUtil.setJsonpResult(wechatT2.getUserinfoByCodeTenant(form), jsonpcallback);
    }
    
    @ResponseBody
    @RequestMapping("/func_get_pay_data")
    @SaveLog(remark = "获取调用微信支付JS-SDK的参数")
    public TongLianPayBackVo getPayData(HttpServletRequest request , String jsonpcallback){
        PayForm form = getSingleParameterMapForBody(request , PayForm.class);
        return VOUtil.setJsonpResult(wechatT2.getPayData(form), jsonpcallback);
    }

}